package com.yuke.cloud.service.pmc.controller.frontend;

import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.pmc.dto.ProdMainAttrDto;
import com.yuke.cloud.service.pmc.dto.ProdSpecInfoAndPriceDto;
import com.yuke.cloud.service.pmc.service.ProdStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.yuke.cloud.common.util.PublicUtil.isNotEmpty;

/**
* Created by zys on 2018/09/27.
*/
@Slf4j
@Api(value = "WEB - ProdProductController", tags = "处理ProdProduct相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/mall/prod/storage")
public class MallProdStorageController extends BaseController {
    @Resource
    private ProdStorageService prodStorageService;

    @GetMapping("/app/category")
    @ApiOperation(httpMethod = "POST", value = "app商品分类页面")
    public Wrapper<List<ProdSpecInfoAndPriceDto>> getProdByCategoryId(@ApiParam(name = "categoryId", value = "商品类别Id") @RequestParam Long categoryId,
                                                                      @ApiParam(name = "shopId", value = "商品类别Id") @RequestParam(required = false) Long shopId) {
        List<ProdSpecInfoAndPriceDto> prodProducts= prodStorageService.getProdStorageByCategoryId(categoryId, shopId);
        return WrapMapper.ok(prodProducts);
    }

    // 根据商品的productId，主属性值组合获取商品相关信息
    @PostMapping("/getSpecByMainAttribute")
    @ApiOperation(httpMethod = "POST", value = "根据获取商品所有的主属性及其值")
    public Wrapper<ProdSpecInfoAndPriceDto> getSpecByMainAttribute(@ApiParam(name = "prodMainAttrDto", value = "请求参数") @RequestBody(required = false) ProdMainAttrDto prodMainAttrDto,
                                                                   @ApiParam(name = "saleType", value = "销售类型") @RequestParam Byte saleType,
                                                                   @ApiParam(name = "shopId", value = "店铺ID") @RequestParam(required = false) Object shopId) {
        Long shopNum = 0L;
        if ( isNotEmpty(shopId) && shopId instanceof Number){
            shopNum = Long.valueOf(shopId.toString());
        }

        return WrapMapper.ok(prodStorageService.getSpecByMainAttribute(prodMainAttrDto, saleType, shopNum));
    }

}
